#include "main.h"


void BSW_EDMA_INIT(void)
{
    PCC_SetClockMode(PCC, DMAMUX0_CLK, true);
    DMAMUX_Init(DMAMUX);    /* Configure DMAMUX */
    EDMA_Init(DMA);
    EDMA_SetHaltCmd(DMA,false); /*不运行在持续连接模式*/
    EDMA_SetHaltOnErrorCmd(DMA,true);   /*DMA运行错误 置位停止位*/
    EDMA_SetChannelArbitrationMode(DMA,EDMA_ARBITRATION_FIXED_PRIORITY);    /*不使能循环模式*/
    EDMA_SetMinorLoopMappingCmd(DMA,true);
}


void BSW_DMAMUX_INIT(uint8_t Dma_Ch,uint8_t DmaMux_Soc)
{
    DMAMUX_SetChannelSource(DMAMUX, Dma_Ch, DmaMux_Soc);
    DMAMUX_SetChannelCmd(DMAMUX, Dma_Ch,true);	/*DMA通道使能 和周期触发位 触发源*/
}


void BSW_DMA_INIT_CFG(BSW_Dma_Cfg Dma_Cfg)
{
    /*设置源地址信息*/
    EDMA_TCDSetSrcAddr(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SrcAddr);     /*设置源地址*/ 
    EDMA_TCDSetSrcOffset(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SrcOffset); /*在执行完针对源地址的操作之后，是否在原地址的基础上累加1*/
    EDMA_TCDSetSrcTransferSize(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SrcTrfSize);  /*设置源地址的传输宽度*/
    EDMA_TCDSetSrcLastAdjust(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SrcLastAdj);    /*主的计数次数（major iteration count）达到后，是否重新更改源地址*/
    /*设置目的地址信息*/
    EDMA_TCDSetDestAddr(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.DestAddr);   /*设置目的地址 */
    EDMA_TCDSetDestOffset(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.DestOffset);   /*在执行完针对目的地址的操作之后，是否在原地址的基础上累加*/
    EDMA_TCDSetDestTransferSize(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.DestTrfSize);    /*设置源地址的传输宽度*/
    EDMA_TCDSetDestLastAdjust(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.DestLastAdj);  /*主的计数次数（major iteration count）达到后，是否重新更改目标地址*/


//	EDMA_TCDSetAttribute(DMA,Dma_Cfg.DMA_Ch,EDMA_MODULO_4B,EDMA_MODULO_OFF,Dma_Cfg.SrcTrfSize,Dma_Cfg.DestTrfSize);


    /*EDMA_TCDSetNbytes(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SetNbytes);*/    /*单次传输数据1byte*/
    DMA->TCD[Dma_Cfg.DMA_Ch].NBYTES.MLNO = Dma_Cfg.SetNbytes;
    EDMA_TCDSetMajorCount(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.SetMjrCount);
    DMA->TCD[Dma_Cfg.DMA_Ch].CSR  = 0;  /*清空CSR的设置 */	
    //DMA->TCD[Dma_Cfg.DMA_Ch].CSR &= 0xFFFFFFFF ^ DMA_TCD_CSR_DONE_MASK;  // Clear Channel Done flag

    /*配置DMA中断*/
    DMA->INT &=~(1<<Dma_Cfg.DMA_Ch);    /*关闭相应通道的中断请求 */ 
    EDMA_TCDSetMajorCompleteIntCmd(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.b_MjrCpltIrq);    /*使能DMA 主循环计数器减到零 中断*/
    EDMA_TCDSetMajorHalfCompleteIntCmd(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.b_HlfCpltIrq);    /*关闭DMA 主循环计数器减到一半 中断*/
    DMA->INT |=(1<<Dma_Cfg.DMA_Ch); /*开启相应通道的中断请求 */ 	

    INT_SYS_EnableIRQ(Dma_Cfg.IRQn);    /*配置NVIC中断向量表*/
    //DMA->TCD[CHn].DREQ 使能DMA硬件请求(DMA_TCD_CSR_DREQ)
    //The channel's ERQ bit is affected:0.not affected,1.affected
    EDMA_TCDSetDisableDmaRequestAfterTCDDoneCmd(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.b_DmaRqstDone);	/*主循环计数器等于零后，自动关闭DMA */
    //DMA->SERQ enable DMA channel n HW trigger 
    //DMA->CERQ disable DMA channel n HW trigger
    EDMA_SetDmaRequestCmd(DMA,Dma_Cfg.DMA_Ch,Dma_Cfg.b_Dma_En); /*使能通道硬件DMA请求  DMA->ERQ = (DMA_ERQ_ERQ0_MASK<<(0));*/	
}


void BSW_DMA_START(uint8_t DMA_CHn)
{
	DMA->INT |=	(1<<DMA_CHn);          	/*清除通道传输中断标志位*/
	DMA->ERQ |= (1<<(DMA_CHn));         /*使能通道硬件DMA请求*/
}

void BSW_DMA_STOP(uint8_t DMA_CHn)
{
  DMA->ERQ &=~(1<<(DMA_CHn));         /*禁止通道硬件DMA请求*/
}

/*主的计数次数（major iteration count）达到后重新更改目标地址*/
void BSW_DMA_DestLastAdjust(uint8_t DMA_CHn,uint16_t Ajust_Byte)
{
    EDMA_TCDSetDestLastAdjust(DMA,DMA_CHn,Ajust_Byte);
}

/*主的计数次数（major iteration count）达到后，是否重新更改源地址*/
void BSW_DMA_SrcLastAdjust(uint8_t DMA_CHn,uint16_t Ajust_Byte)
{
    EDMA_TCDSetSrcLastAdjust(DMA,DMA_CHn,Ajust_Byte);
}
/*********重新启动DMAINIT**********/
void BSW_DMA_REINIT(uint8_t DMA_CHn,uint32_t SourceAddr,uint32_t TargetAddr,uint16_t Send_Buff_Byte)
{
	EDMA_SetDmaRequestCmd(DMA,DMA_CHn,false);
	DMA->TCD[DMA_CHn].CSR &= ~DMA_TCD_CSR_START_MASK;	/*DMA->SSRT &= (uint8_t)(~Dma_Cfg.DMA_Ch);*/	
	DMA->TCD[DMA_CHn].SADDR = SourceAddr;							/*设置源地址*/ 
	DMA->TCD[DMA_CHn].DADDR = TargetAddr;							/*设置目标地址*/
//	DMA->TCD[DMA_CHn].NBYTES.MLNO = Send_Buff_Byte;		/*单次传输数据大小单位/byte*/
	EDMA_TCDSetMajorCount(DMA,DMA_CHn,Send_Buff_Byte);
	DMA->TCD[DMA_CHn].CSR |= DMA_TCD_CSR_START_MASK;	/*DMA->SSRT |= (uint8_t)Dma_Cfg.DMA_Ch;*/
//	EDMA_SetDmaRequestCmd(DMA,DMA_CHn,true);
}

/*********下次传输初始化**********/
void BSW_DMA_NEXT_INIT(uint8_t DMA_CHn,uint32_t TargetAddr,uint16_t Send_Buff_Byte)
{
	EDMA_SetDmaRequestCmd(DMA,DMA_CHn,false);
	DMA->TCD[DMA_CHn].CSR &= ~DMA_TCD_CSR_START_MASK;	/*DMA->SSRT &= (uint8_t)(~Dma_Cfg.DMA_Ch);*/	
	DMA->TCD[DMA_CHn].DADDR = TargetAddr;							/*设置目标地址*/
//	DMA->TCD[DMA_CHn].NBYTES.MLNO = Send_Buff_Byte;		/*单次传输数据大小单位/byte*/
	EDMA_TCDSetMajorCount(DMA,DMA_CHn,Send_Buff_Byte);
	DMA->TCD[DMA_CHn].CSR |= DMA_TCD_CSR_START_MASK;	/*DMA->SSRT |= (uint8_t)Dma_Cfg.DMA_Ch;*/
//	EDMA_SetDmaRequestCmd(DMA,DMA_CHn,true);
}





void DMA0_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH0);
	DMA0_TCD_DONE_IRQ();
	
}


void DMA1_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH1);
	DMA1_TCD_DONE_IRQ();
}


void DMA2_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH2);
	DMA2_TCD_DONE_IRQ();
}


void DMA3_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH3);
	DMA3_TCD_DONE_IRQ();
}


void DMA4_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH4);
	DMA4_TCD_DONE_IRQ();
}


void DMA5_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH5);
	DMA5_TCD_DONE_IRQ();
}


void DMA6_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH6);
	DMA6_TCD_DONE_IRQ();
}


void DMA7_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH7);
	DMA7_TCD_DONE_IRQ();
}

void DMA8_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH8);
	DMA8_TCD_DONE_IRQ();
}


void DMA9_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH9);
	DMA9_TCD_DONE_IRQ();
}


void DMA10_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH10);
	DMA10_TCD_DONE_IRQ();
}


void DMA11_IRQHandler(void)
{
	EDMA_ClearIntStatusFlag(DMA,DMA_CH11);
	DMA11_TCD_DONE_IRQ();
}



